package com.xywy.common.net;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * 网络判断
 * 
 * @author Joker
 * 
 */
public class NetWorkUtil {

	/**
	 * 检查网络
	 * 
	 * @param context
	 * @return
	 */
	public static boolean checkNetWork(Context context) {
		// ConnectivityManager--系统服务
		// ①判断WIFI链接
		boolean isWIFI = isWIFIConnectivity(context);
		// ②判断MOBILE链接
		boolean isMOBILE = isMOBILEConnectivity(context);

		// 如果没有网络
		if (!isMOBILE && !isWIFI) {
			return false;
		}

		return true;
	}

	/**
	 * 判断MOBILE链接
	 * 
	 * @param context
	 * @return
	 */
	private static boolean isMOBILEConnectivity(Context context) {
		ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		// MOBILE链接的描述信息
		NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		if (networkInfo != null) {
			return networkInfo.isConnected();
		}
		return false;
	}

	/**
	 * 判断WIFI链接状态
	 * 
	 * @param context
	 * @return
	 */
	private static boolean isWIFIConnectivity(Context context) {
		ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		// WIFI链接的描述信息
		NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if (networkInfo != null) {
			return networkInfo.isConnected();
		}
		return false;
	}
}
